package com.ycx.furns.web;

import com.google.code.kaptcha.servlet.KaptchaServlet;
import com.google.gson.Gson;
import com.ycx.furns.entity.Furn;
import com.ycx.furns.entity.Member;
import com.ycx.furns.entity.Page;
import com.ycx.furns.service.FurnService;
import com.ycx.furns.service.MemberService;
import com.ycx.furns.service.impl.FurnServiceImpl;
import com.ycx.furns.service.impl.MemberServiceImpl;
import com.ycx.furns.utils.DataUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;

@WebServlet("/memberServlet")
public class MemberServlet extends BasicServlet {
    MemberService memberService = new MemberServiceImpl();

    protected void checkRegisterName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        Gson gson = new Gson();
        Map<String, Object> map = new HashMap<>();
        boolean isExists = memberService.registerCheck(name);
        map.put("isExists", isExists);
        String mapJson = gson.toJson(map);
        resp.getWriter().write(mapJson);

    }

    public void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().removeAttribute("member");
        resp.sendRedirect("/jiaju_mall");
    }

    public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("user-name");
        String pwd = req.getParameter("user-password");


        String code = req.getParameter("code");
        String token = (String) req.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        req.getSession().removeAttribute(KAPTCHA_SESSION_KEY);
        if (token != null && token.equalsIgnoreCase(code)) {
            Member member = memberService.loginCheck(name, pwd);
            if (member != null) {
                System.out.println("用户名密码验证正确，可以登录");
                req.getSession().setAttribute("member", member);
//                req.getRequestDispatcher("/views/member/login_ok.jsp").forward(req, resp);
                resp.sendRedirect(req.getContextPath() + "/views/member/login_ok.jsp");
            } else {
                req.setAttribute("username", name);
                req.setAttribute("msg", "用户名或密码错误");
                req.getRequestDispatcher("/views/member/login.jsp").forward(req, resp);
            }
        } else {
            req.setAttribute("msg", "验证码不正确");
            req.getRequestDispatcher("/views/member/login.jsp").forward(req, resp);
        }
    }

    public void register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("registerServlet 被调用~");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String email = req.getParameter("email");
        String code = req.getParameter("code");
        String token = (String) req.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        req.getSession().removeAttribute(KAPTCHA_SESSION_KEY);
        if (token != null && token.equalsIgnoreCase(code)) {

            if (memberService.registerCheck(username)) {
                System.out.println("用户名可用，可以注册");
                if (memberService.registerMember(new Member(null, username, password, email))) {
                    System.out.println("注册成功~");
//                    req.getRequestDispatcher("/views/member/register_ok.jsp").forward(req,resp);
                    resp.sendRedirect(req.getContextPath() + "/views/member/register_ok.jsp");
                } else {
                    req.getRequestDispatcher("/views/member/register_fail.jsp").forward(req, resp);
                }
            } else {
                System.out.println("用户名已存在");
                req.setAttribute("msg", "用户名已存在");
                req.setAttribute("action", "register");
                req.getRequestDispatcher("/views/member/login.jsp").forward(req, resp);
            }
        } else {
            req.setAttribute("msg", "验证码不正确");
            req.setAttribute("action", "register");
            req.getRequestDispatcher("/views/member/login.jsp").forward(req, resp);
        }
    }
}
